{%-import 'loader.jinja2' as loader-%}

{%-if 'items' in schema-%}
    {%-if '$ref' in schema['items']-%}
        {%-set itemType = resolver.py_class_name(schema['items']['$ref'])-%}
    {%-endif-%}
{%-endif-%}

{%-set fullClassPath -%}
    {%-if parentClass is not defined or parentClass is none-%}
        {{Name}}
    {%-else-%}
        {{parentClass}}.{{Name}}
    {%-endif-%}
{%-endset-%}

class {{Name}} (UserList):
    """ This represents a JSON array.
    """
    {%-if 'items' in schema%}
    {%-if '$ref' not in schema['items']%}
    {%-set itemType = 'self.Item'%}
    {{loader.Class(resolver, 'Item', schema['items'], fullClassPath) |indent(4)}}
    {%-endif%}
    {%-endif%}
    
    def __init__(self, the_list=None):
        """Initializer for array.
        """
        if not hasattr(the_list, '__iter__'):
            raise TypeError("The provided list was not iterable")

        self.the_list = the_list

        if isinstance(the_list, type(self)):
            super().__init__(the_list.data)
        else:
            {%-if schema.minItems is defined%}
            if len(the_list) < {{schema.minItems}}:
                raise ValueError("Provided array is too short")
            {%-endif%}
            {%-if schema.maxItems is defined%}
            if len(the_list) > {{schema.maxItems}}:
                raise ValueError("Provided array is too long")
            {%-endif%}
            super().__init__([{{itemType}}(x) for x in the_list])

    def Append(self, new_value) -> {{Name}}:
        {%-if schema.maxItems is defined%}
        if (len(self.the_list)+1) > {{schema.maxItems}}:
            raise ValueError("Appending would make the array too long")
        {%-endif%}
        self.data.append({{itemType}}(new_value))
        return self

    def Serializable(self) -> list:
        return self.data

